package com.aube.plugin.episode;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.DynamicLoaderHelper;
import com.huyn.baseframework.dynamicload.IPositionListener;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Category;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/9/27.
 */
public class GridVideoGroup extends DLLayout implements VideoGroup {

    private List<IPositionListener> mAnimItems = new ArrayList<>();
    private AtomicBoolean mAnimated = new AtomicBoolean(false);
    private View[] mGuideTarget;

    private VideoPager mVideoPager;

    public GridVideoGroup(Context context) {
        this(context, null);
    }

    public GridVideoGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GridVideoGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        mVideoPager = new VideoPager(context);
        addView(mVideoPager, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mVideoPager.setOnPageChangedListener(new VideoPager.OnPageChangedListener() {
            @Override
            public void onPageChanged(int whichScreen, View view) {

            }
        });
    }

    @Override
    public void fillData(Object o) {
    }

    public void initData(List<VideoModel> list, Category category, IUmengListener listener) {
        if(list == null || list.size() == 0)
            return;
        int size = list.size();
        int pageSize = size / 4 + ((size % 4) > 0 ? 1 : 0);
        mGuideTarget = new View[2];
        for(int i=0; i<pageSize; i++) {
            VideoGrid mGrid = (VideoGrid) DynamicLoaderHelper.loadTargetLayout(context, getDLPluginPackage(), VideoGrid.class.getName());
            mVideoPager.addView(mGrid);
            int start = i*4;
            int end = (i+1)*4;
            mGrid.setDLBridge(mBridge);
            mGrid.initData(list.subList(start, end >= size ? size : end), category, listener);
            mAnimItems.add(mGrid);
            if(i==0)
                mGuideTarget = mGrid.getGuideTarget();
        }
    }

    @Override
    public void notifyAnim(int i) {
        if(mAnimated.get())
            return;
        mAnimated.set(true);
        if(mAnimItems == null)
            return;
        notifyItemAnim(i + 300);
    }

    private void notifyItemAnim(int animDelay) {
        int i=0;
        for(IPositionListener item : mAnimItems) {
            item.notifyAnim(animDelay + i*600);
            i++;
        }
    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }

    @Override
    public View[] getGuideTarget() {
        return mGuideTarget;
    }
}
